package com.groupwork.meeting.service.impl;

import com.groupwork.meeting.Mapper.RoleMapper;
import com.groupwork.meeting.entity.Role;
import com.groupwork.meeting.entity.User;
import com.groupwork.meeting.service.IRoleService;
import com.groupwork.meeting.service.ex.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 鱼凫
 * @version 1.0
 * Create by 2021/11/2 9:17
 * @Description //TODO $role模块业务层实现类
 */

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Role roleGetByUid(String uid) {
        Role result = roleMapper.ridFindByUid(uid);
        if (result == null){
            throw new RoleNotFoundException("角色数据不存在");
        }

        Role role = new Role();
        role.setRoleName(result.getRoleName());
        role.setRoleId(result.getRoleId());

        return role;
    }

    @Override
    public Role roleGetByUname(String uname) {
        Role result = roleMapper.ridFindByUname(uname);
        if (result == null){
            throw new UserRoleNotFoundException("该用户尚未分配角色");
        }
        Role role = new Role();
        role.setRoleName(result.getRoleName());
        role.setRoleId(result.getRoleId());
        return role;
    }

    @Override
    public List<Role> getRoleList() {
        List<Role> list = roleMapper.findRoleList();
        for (Role role : list){

        }
        return list;
    }

    @Override
    public void deleteRole(String rname) {
        Role result = roleMapper.findByRname(rname);
        if (result == null){
            throw new RoleNotFoundException("角色数据不存在");
        }
        Integer rows = roleMapper.deleteByRname(rname);
        if (rows != 1){
            throw new DeleteException("删除数据时产生未知异常");
        }
    }

    @Override
    public Role getByRid(String rname) {
        Role result = roleMapper.findByRname(rname);
        if (result == null){
            throw new RoleNotFoundException("角色数据不存在");
        }
        Role role = new Role();
        role.setRoleId(result.getRoleId());
        role.setRoleName(result.getRoleName());
        return role;
    }

    @Override
    public void addRole(Role role) {
        String roleid = role.getRoleId();
        String rolename = role.getRoleName();
        String roledescribe = role.getRoleDescribe();
        Role result = roleMapper.findByRname(rolename);
        if (result != null){
            throw new RoleNameDoplicatedException("尝试添加的角色名"+ rolename + "已经被占用");
        }
        Integer rows = roleMapper.addRole(role);
        if (rows != 1){
            throw new InsertException("添加角色时产生未知异常");
        }
    }

    @Override
    public List<Role> roleFoundByUname(String username) {
        List<Role> list = roleMapper.roleFindByUname(username);
        for (Role role : list){

        }
        return list;
    }
}
